package com.qf.j2112.controller;

import java.util.List;
import com.qf.j2112.po.SysGoods;
import com.qf.j2112.service.SysGoodsService;
import com.qf.j2112.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 商品管理Controller
 * @author j2112
 * @date 2022-04-12
 */
@RestController
@CrossOrigin
public class SysGoodsController {

    @Autowired
    private SysGoodsService sysGoodsService;

    /* 查询首页热门商品列表*/
    @RequestMapping(value ="/hotlist",method = RequestMethod.GET)
    public AjaxResult listHot() {
        List<SysGoods> list = sysGoodsService.selectHotSysGoodsList();
        return AjaxResult.success("查询成功",list);
    }

    /* 全查商品*/
    @RequestMapping(value ="/listAll",method = RequestMethod.GET)
    public AjaxResult listAll() {
        List<SysGoods> list = sysGoodsService.selectAll();
        return AjaxResult.success("查询成功",list);
    }

    /* 根据类型查询商品*/
    @RequestMapping(value ="/listType",method = RequestMethod.GET)
    public AjaxResult list(Integer type) {
        List<SysGoods> list = sysGoodsService.selectAllByType(type);
        return AjaxResult.success("查询成功",list);
    }


    /*获取商品详细信息*/
    @RequestMapping(value ="/getgoods/{goodsId}",method = RequestMethod.GET)
    public AjaxResult getInfo(@PathVariable("goodsId") Integer goodsId) {
        return AjaxResult.success(sysGoodsService.selectSysGoodsByGoodsId(goodsId));
    }

    /*获取商品详细信息,给其他服务调用*/
    @RequestMapping(value ="/goods/{goodsId}",method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = "application/json;charset=UTF-8")
    public SysGoods getGoodsInfo(@PathVariable("goodsId") Integer goodsId) {
        SysGoods sysGoods=sysGoodsService.selectSysGoodsByGoodsId(goodsId);
        return sysGoods;
    }

    /*新增商品*/
    @RequestMapping(value ="/add",method = RequestMethod.POST)
    public AjaxResult add(@RequestBody SysGoods sysGoods) {
        return AjaxResult.success("新增成功",sysGoodsService.insertSysGoods(sysGoods));
    }

    /*修改商品管理*/
    @RequestMapping(value ="/edit",method = RequestMethod.PUT)
    public AjaxResult edit(@RequestBody SysGoods sysGoods)
    {
        return AjaxResult.success("编辑成功",sysGoodsService.updateSysGoods(sysGoods));
    }

    /*删除商品*/
    @RequestMapping(value ="/{goodsIds}",method = RequestMethod.DELETE)
    public AjaxResult remove(@PathVariable Integer[] goodsIds) {
        return AjaxResult.success("删除成功",sysGoodsService.deleteSysGoodsByGoodsIds(goodsIds));
    }
}
